from flask import Blueprint, request, jsonify
from app.models import User  # 假设你已经有 User 模型
from app import db

router = Blueprint('users', __name__)

@router.route('/register', methods=['POST'])
def register():
    data = request.get_json()

    # 获取用户数据
    username = data.get('username')
    email = data.get('email')
    password = data.get('password')

    # 简单验证
    if not username or not email or not password:
        return jsonify({'message': '用户名、邮箱和密码是必需的！'}), 400

    # 检查用户名和邮箱是否已存在
    existing_user = User.query.filter((User.username == username) | (User.email == email)).first()
    if existing_user:
        return jsonify({'message': '用户名或邮箱已存在！'}), 400

    # 创建新用户
    new_user = User(username=username, email=email, password=password)
    db.session.add(new_user)
    db.session.commit()

    return jsonify({'message': '注册成功！'}), 201
